//
//  AlarmLearnViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/27.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "AlarmLearnViewController.h"

#import "AlarmTableViewCell.h"
#import "AlarmFirstTableViewCell.h"
#import "AlarmSecondTableViewCell.h"

#import "ReplyViewController.h"

#import <MessageUI/MessageUI.h>//系统短信


#define identifier @"identifier"
@interface AlarmLearnViewController ()<UITableViewDataSource,UITableViewDelegate,AlarmFirstTableViewCellDelegate,MFMessageComposeViewControllerDelegate>
{
    NSArray *cellNameArr;
    
    NSString *msgTitle;//发送短信内容

}
@property (nonatomic, strong) UITableView *tableView;
/** 是否展开 */
@property (nonatomic , strong) NSMutableArray *openArray;
/** 被选择的模式和数字存储 */
@property (nonatomic , strong) NSMutableArray *rightArray;
/** 第二个数字选择 数字数组 */
@property (nonatomic , strong) NSArray *numberArray;
/**  */
//@property (nonatomic , strong) NSString *firstString;
/**  */
//@property (nonatomic , strong) NSString *secondString;

@end

@implementation AlarmLearnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"报警学习";
    
    cellNameArr = @[@"选择模式",@"数字选择"];
    
    self.openArray = @[@(0),@(0)].mutableCopy;
    self.rightArray = @[@"",@""].mutableCopy;
    self.numberArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
    [self addTableviewUI];
    
    [self addBottomView];
}
- (void)addTableviewUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight-kNavigationBarHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"AlarmFirstTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"AlarmSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
}

- (void)addBottomView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 160)];
    view.backgroundColor = BaseColor;
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(15, 35, kUIScreenWidth-30, 50);
    [btn setBackgroundImage:[UIImage imageNamed:@"ann"] forState:UIControlStateNormal];
    [btn setTitle:@"开始学习" forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = [UIColor grayColor].CGColor;
    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(study) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:btn];
    
//    UIButton *btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    btn2.frame = CGRectMake(15, 75, kUIScreenWidth-30, 50);
//    btn2.backgroundColor = [UIColor whiteColor];
//    [btn2 setTitle:@"短信回复" forState:(UIControlStateNormal)];
//    btn2.titleLabel.font = [UIFont systemFontOfSize:17];
//    [btn2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
////    btn2.layer.borderWidth = 1;
////    btn2.layer.cornerRadius = 5;
////    btn2.layer.borderColor = [UIColor grayColor].CGColor;
//    [btn2 addTarget:self action:@selector(reply) forControlEvents:(UIControlEventTouchUpInside)];
//    [view addSubview:btn2];
    
    self.tableView.tableFooterView = view;
}

- (void)study{
    
    //
    [self sendMsg];
    
}

//- (void)reply{
//    ReplyViewController *replyVC = [[ReplyViewController alloc] init];
//    [self.navigationController pushViewController:replyVC animated:YES];
//}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
            return 100;
            
    }else {
        
            return 50;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [self.openArray[0] boolValue]==NO?0:1;
    }
    return [self.openArray[1] boolValue]==NO?0:self.numberArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return cellNameArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AlarmFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        
        cell.string = self.rightArray[0];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    } else {
        AlarmSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.label1.text = self.numberArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
}

- (void)passString:(NSString *)string{
    self.rightArray[0] = string;
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 70;
    }else {
        return 70;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 70)];
    headerView.backgroundColor = BaseColor;
    
    AlarmTableViewCell *cell = [AlarmTableViewCell initWithAlarmTableViewCell];
    [headerView addSubview:cell];
    cell.backgroundColor = [UIColor whiteColor];
    cell.frame = CGRectMake(0, 10, kUIScreenWidth, 60);
    cell.leftLable.text = cellNameArr[section];
    cell.rightLable.text = self.rightArray[section];
    NSString *imageName = [self.openArray[section] boolValue]==YES?@"bjxx_jt1":@"bjxx_jt";
    cell.rightImage.image = [UIImage imageNamed:imageName];
    cell.tag = 3000+section;
    
    [cell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSection:)]];
    
    return headerView;
}

- (void)tapSection:(UITapGestureRecognizer *)tap{
    NSInteger index = tap.view.tag - 3000;
    BOOL isOpen = [self.openArray[index] boolValue];
    self.openArray[index] = @(!isOpen);
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    return footview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        self.rightArray[1] = self.numberArray[indexPath.row];
        self.openArray[1] = @"0";
        [self.tableView reloadData];
    }
}


#pragma mark -------------- 发送短信 ----------------
-(void)sendMsg{
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSString *telStr = saveDic[@"locknumber"];
//    telStr = @"10086";
    
    NSString *modelStr = _rightArray[0];
    NSString *numberStr = _rightArray[1];
    if (modelStr.length && numberStr.length) {
        
        if (telStr.length>4) {
            
            if ([modelStr isEqualToString:@"红外"]) {
                msgTitle = [NSString stringWithFormat:@"redlearn:%@",numberStr];
            }else if ([modelStr isEqualToString:@"烟雾"]){
                msgTitle = [NSString stringWithFormat:@"smokelearn:%@",numberStr];
            }else if ([modelStr isEqualToString:@"燃气"]){
                msgTitle = [NSString stringWithFormat:@"gaslearn:%@",numberStr];
            }else if ([modelStr isEqualToString:@"门磁"]){
                msgTitle = [NSString stringWithFormat:@"magnetlearn:%@",numberStr];
            }else if ([modelStr isEqualToString:@"滴水"]){
                msgTitle = [NSString stringWithFormat:@"waterlearn:%@",numberStr];
            }
            
            
            [self showMessageView:[NSArray arrayWithObjects:telStr, nil] title:nil body:msgTitle];
            
            
        }else{
            [MBProgressHUD showText:@"请先设置门锁号码"];
        }
        
    }else{
        [MBProgressHUD showText:@"请完善信息"];
    }
    
    
    
}
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        
        
        [self presentViewController:controller animated:YES completion:nil];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"该设备不支持短信功能 " delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - MFMessageComposeViewController

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    if (result) {
        if (MessageComposeResultSent) {//短信发送成功
            
            [MBProgressHUD showText:@"发送成功"];
            
        }
    }else{
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
